import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:time/models/SetModel/SetModel.dart';

import '../../common/NotificationUtils.dart';

/// 创建时间：2024/1/24
/// 作者：wangjianhua
/// 描述：

class NotiSet extends StatelessWidget {
  NotiSet({super.key, required this.notificationUtils});
  NotificationUtils notificationUtils;

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MyHomePage(title: 'Time', notificationUtils: this.notificationUtils);
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({super.key, required this.title, required this.notificationUtils});
  final String title;
  NotificationUtils notificationUtils;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<SetModel> sets = getSetModels();

  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  final NotificationUtils _notificationUtils = NotificationUtils();

  SnackBar getSnackBar(String title) {
    final snackBar = SnackBar(
      content: Text(title),
      elevation: 15,
      behavior: SnackBarBehavior.floating,
      action: SnackBarAction(
        label: '知道了',
        onPressed: ()  {
        },
      ),
    );
    return snackBar;
  }


  List<Widget> lists() {
    List<Widget> l = [];

    for (SetModel set in sets) {
      l.add(Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              set.title,
              style: const TextStyle(
                  color: Colors.black,
                  fontSize: 20,
                  fontWeight: FontWeight.w300),
            ),
            Switch(
                value: set.switchStatue,
                onChanged: (value) {
                  setState(() {
                    set.switchStatue = value;
                    if (set.title == '通知开关') {
                      ScaffoldMessenger.of(context).clearSnackBars();
                      if (set.switchStatue) {
                        ScaffoldMessenger.of(context)
                            .showSnackBar(getSnackBar('您已打开通知'));
                      } else {
                        ScaffoldMessenger.of(context)
                            .showSnackBar(getSnackBar('您已关闭通知'));
                        flutterLocalNotificationsPlugin.cancelAll();
                      }
                    }
                  });
                })
          ],
        ),
      ));
    }
    return l;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text("设置"),
        leading: IconButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            icon: const Icon(Icons.arrow_back_ios)),
      ),
      body: Center(
          child: ListView(
        shrinkWrap: false,
        // 竖直方向上布局
        scrollDirection: Axis.vertical,
        padding: const EdgeInsets.all(10),
        // 每个自组件的高度
        itemExtent: 50,
        children: lists(),
      )),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
